using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SS.core
{
	internal class unit
	{
		private Func objFunc = new Func();
		private string strConnection = "";
		public unit()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public void loadToListView(ListView lv)
		{
			lv.Items.Clear();
			int num = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabUnitProduct] ORDER BY [UnitName]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				lv.Items.Add((num + 1).ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add(sqlDataReader["UnitID"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["UnitName"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["UnitNotes"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void loadToDataGridView(DataGridView dgv, BindingNavigator bdn)
		{
			dgv.AutoGenerateColumns = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string selectCommandText = "SELECT * FROM [TabUnitProduct] ORDER BY [UnitName]";
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, sqlConnection);
			DataSet dataSet = new DataSet();
			sqlDataAdapter.Fill(dataSet, "TabUnitProduct");
			BindingSource bindingSource = new BindingSource();
			bindingSource.DataSource = dataSet.Tables["TabUnitProduct"];
			bdn.BindingSource = bindingSource;
			dgv.DataSource = bindingSource;
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getInfo(string ID, TextBox TxtGpName, TextBox TxtGpNote)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabUnitProduct] WHERE [UnitID] = " + ID.ToString();
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				TxtGpName.Text = sqlDataReader["UnitName"].ToString();
				TxtGpNote.Text = sqlDataReader["UnitNotes"].ToString();
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int del(string id)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("DelUnitProduct", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar);
				sqlCommand.Parameters["ID"].Value = id;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
	}
}
